package gdma.hqdes.webapp.task;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-11-11
 */

import gdma.hqdes.bean.HqdProject;
import gdma.hqdes.bean.Unit;
import gdma.hqdes.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ScheduledTasks implements ApplicationRunner {

    @Autowired
    ProjectService projectService;

    @Scheduled(cron = "0 0 * * * *")
    public void updateActiveProjectsProgress() {
        log.info("开始更新活跃项目的进度：");
        List<HqdProject> projects = projectService.getActiveProjects(new Date(), Unit.SCOPE_MUNICIPAL);
        for(HqdProject project : projects) {
            float progress = projectService.updateProjectProgress(project.getId());
            log.info(project.getName() + ": " + progress);
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        updateActiveProjectsProgress();
    }

}
